/**   
 * @Title: LocalFragment.java 
 * @Package: com.rename.icode.activity 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午2:45:06 
 * @version  
 */

package com.rename.icode.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.rename.icode.R;
import com.rename.icode.comm.Constants;
import com.rename.icode.manager.UserManager;
import com.rename.icode.model.CodeFile;
import com.rename.icode.utils.LogUtil;
import com.rename.icode.view.CodeFilesAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-11 下午2:45:06
 * @version V1.0
 */

public class LocalFragment extends Fragment {

	private static final String TAG = "LocalFragment";
	private View mLocalFragmentView;
	private static LocalFragment mLocalFragment;
	private Context mContext;

	private ListView mFileList;
	private OnClickListener mFileOnClickListener;
	private CodeFilesAdapter mAdapter;
	private List<CodeFile> mCodeFiles = new ArrayList<CodeFile>();
	private LocalReceiver mReceiver = null;

	public LocalFragment() {
		super();
	}

	public static LocalFragment getLocalFragmentIntance() {
		if (mLocalFragment != null) {
			return mLocalFragment;
		} else {
			mLocalFragment = new LocalFragment();
			return mLocalFragment;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mLocalFragmentView = inflater.inflate(R.layout.local_info, container,
				false);

		init();

		return mLocalFragmentView;
	}

	private void init() {
		LogUtil.i(TAG, "LocalFragment init...");
		mContext = getActivity();

		mFileOnClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				//TODO:打开代码编辑页
				CodeFile codeFile = (CodeFile)v.findViewById(R.id.file_name_txt).getTag();
				LogUtil.i("gg", "文件名：" + codeFile.getName());
				
				((MainActivity)mContext).changeLayout(MainActivity.CODE);//切换到代码编辑界面
				((MainActivity)mContext).showCodeViewByLanguage(true, codeFile);//打开指定代码
			}
		};
		mAdapter = new CodeFilesAdapter(mContext, mCodeFiles,
				CodeFilesAdapter.SHOW_MODE);
		mAdapter.setOnClickListener(mFileOnClickListener);
		mFileList = (ListView) mLocalFragmentView.findViewById(R.id.local_list);
		mFileList.setAdapter(mAdapter);

	}

	private class LocalReceiver extends BroadcastReceiver {

		/**
		 * Description
		 * 
		 * @param context
		 * @param intent
		 * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
		 *      android.content.Intent)
		 */

		@Override
		public void onReceive(Context context, Intent intent) {
			LogUtil.i("wxm", "接受到了广播: " + intent.getAction());
			String action = intent.getAction();

			if (Constants.LOCAL_REFRESH.equals(action)) {
				LogUtil.i(TAG, "LOCAL_REFRESH...");
				refreshCodeFileListView();
			}else if(Constants.PAGE_LOAD_FINISHED.equals(action)){
				LogUtil.i(TAG, "PAGE_LOAD_FINISHED...");
				
			}
		}

	}

	private void initReceiver() {
		if (null == mReceiver) {
			mReceiver = new LocalReceiver();
			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction(Constants.LOCAL_REFRESH);
			intentFilter.addAction(Constants.PAGE_LOAD_FINISHED);
			mContext.registerReceiver(mReceiver, intentFilter);
		}
	}

	private void refreshCodeFileListView() {
		mCodeFiles = UserManager.getInstance().getUser().getLocalData();
		mAdapter.refreshList(mCodeFiles);
	}

	@Override
	public void onResume() {
		super.onResume();
		initReceiver();
		refreshCodeFileListView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mContext.unregisterReceiver(mReceiver);
		mReceiver = null;
	}

}
